#include "VPManagerFileItem.h"


CVPlayerManagerFileItem::CVPlayerManagerFileItem()
{
	InitializeCriticalSection(&m_csOperation);
	//jdhuang 20090604	
//	m_nSelectedItem = 0;

}

CVPlayerManagerFileItem::~CVPlayerManagerFileItem()
{
	RemoveAll();
	DeleteCriticalSection(&m_csOperation);
}

void CVPlayerManagerFileItem::AddItem(CVPFileItem *pItem)
{
	return InsertItem(pItem, -1);
}


void CVPlayerManagerFileItem::InsertItem(CVPFileItem *pItem, int nPos)
{
	EnterCriticalSection(&m_csOperation);
	if (nPos<0 || nPos>=GetItemCount())
	{
		m_Items.push_back(pItem);
	}
	else if (nPos==0)
	{
		m_Items.insert(m_Items.begin(), pItem);
	}
	else
	{
		CVPFileItems::iterator it;
		int i = 0;
		for (it=m_Items.begin();it!=m_Items.end();it++,i++)
		{
			if(i==nPos)
			{
				m_Items.insert(it, pItem);
				break;
			}
		}
	}
	LeaveCriticalSection(&m_csOperation);
}


void CVPlayerManagerFileItem::RemoveItem(int nIndex)
{
	if(nIndex<-1 || nIndex>=GetItemCount())
		return;

	EnterCriticalSection(&m_csOperation);
	if(nIndex==-1)
	{
		//delete (DesktopItem *)(m_Items[GetItemCount() /*-1*/]);
		OnRemoveItem(GetItemCount()-1);
		m_Items.pop_back();
	}
	else
	{
		CVPFileItems::iterator it;
		int i = 0;
		for (it=m_Items.begin();it!=m_Items.end();it++,i++)
		{
			if(i==nIndex)
			{
				//delete (DesktopItem *)(&m_Items[i]);
				OnRemoveItem(nIndex);
				m_Items.erase(it);
				break;
			}
		}
	}
	LeaveCriticalSection(&m_csOperation);
}


void CVPlayerManagerFileItem::RemoveAll()
{
	OnRemoveItem(-1);
	EnterCriticalSection(&m_csOperation);
	m_Items.clear();
	LeaveCriticalSection(&m_csOperation);
}

void CVPlayerManagerFileItem::OnRemoveItem(int nIndex)
{
	if(nIndex == -1)
	{
		for(int i = 0; i < GetItemCount(); i++)
		{
			delete GetItem(i);
		}
	}
	else
	{
		delete GetItem(nIndex);
	}
}


CVPFileItem* CVPlayerManagerFileItem::GetItem(int index)
{
	if(index >= GetItemCount() || index < 0)
		return NULL;

	return  m_Items[index];
}
//jdhuang 20120817
/*
void CVPlayerManagerFileItem::unloadFileList()
{	

	TFileList_PLAY::iterator		it;
	PFILELISTDATA_PLAY	pData;
	

	while(m_FileList.size()) 
	{
		it = m_FileList.begin();
		pData = &(*it);
		if (pData->pDirPath  != NULL) 
		{
			delete[] pData->pDirPath;
			pData->pDirPath = NULL;
		}
		if (pData->pFileName != NULL) 
		{
			delete[] pData->pFileName;
			pData->pFileName = NULL;
		}
		m_FileList.erase(it);
	}

	m_SelectedIndex = -1;
	m_PageIndex = -1;

//	m_FileStartIndex = m_FileList.end();
}	// CPlayListManager::unloadFileList
*/
//jdhuang 20120817
/*
void CVPlayerManagerFileItem::LoadPlayListFromExtStorage()
{

	DWORD	dwIndex;
	DWORD	dwTotalList;
	CMarkup FileListXML;
	PFILELISTDATA_PLAY	pListData;
	WCHAR	szXmlFile[MAX_PATH];
	WCHAR	szXmlDefalutFile[MAX_PATH];
	
	unloadFileList();
	GetCurrenXMLFile(&szXmlFile[0], &szXmlDefalutFile[0]);
	if( !FileListXML.Load(szXmlFile) )
	{
		if( !FileListXML.Load(szXmlDefalutFile) )
			return;
	}
	FileListXML.ResetMainPos();
	FileListXML.FindElem(L"LISTS");
	dwTotalList = ::_ttoi(FileListXML.GetAttrib(L"TotolList").c_str());
	FileListXML.IntoElem();

	while(FileListXML.FindElem(L"LIST"))
	{
		pListData = new FILELISTDATA_PLAY;
		ZeroMemory(pListData, sizeof(FILELISTDATA_PLAY));
		FileListXML.GetData().c_str();
		if (FileListXML.FindChildElem(L"FileType") )
		{
			dwIndex = ::_ttoi(FileListXML.GetChildAttrib(L"index").c_str());
			pListData->fileType = static_cast<FILE_TYPE_PLAY>(::_ttoi(FileListXML.GetChildData().c_str()));
		}

		if (FileListXML.FindChildElem(L"FileDir") )
		{
			dwIndex = ::_ttoi(FileListXML.GetChildAttrib(L"index").c_str());
			pListData->pDirPath = new TCHAR[lstrlen(FileListXML.GetChildData().c_str()) + 1];
			::lstrcpy(pListData->pDirPath, FileListXML.GetChildData().c_str());
		}

		if( FileListXML.FindChildElem(L"FileName") )
		{
			dwIndex = ::_ttoi(FileListXML.GetChildAttrib(L"index").c_str());
			pListData->pFileName = new TCHAR[lstrlen(FileListXML.GetChildData().c_str()) + 1];
			::lstrcpy(pListData->pFileName, FileListXML.GetChildData().c_str());
		}

		if (FileListXML.FindChildElem(L"PlayTime") )
		{
			dwIndex = ::_ttoi(FileListXML.GetChildAttrib(L"index").c_str());
			pListData->PlayTime = static_cast<UINT>(::_ttoi(FileListXML.GetChildData().c_str()));
		}

		//if(pListData->fileType == m_enFILETYPE)
		{
			m_FileList.push_back(*pListData);
			delete pListData;
		}
	}

	FileListXML.OutOfElem();
	std::sort(m_FileList.begin(), m_FileList.end());

	RemoveAll();

}
*/